package com.gccloud.design;


import com.gccloud.design.dto.SSPrincipleSaveDto;
import com.gccloud.design.dto.SSPrincipleUpdateDto;
import com.gccloud.design.service.SdPrincipleService;
import com.gccloud.design.vo.BDValueVo;
import com.gccloud.design.vo.PrincipleAttrVO;
import com.gccloud.design.vo.SdPrincipleVO;
import com.gccloud.starter.common.dto.SearchDTO;
import com.gccloud.starter.common.exception.GlobalException;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author mengyu
 * @date 2020/12/25 14:46
 */
@Api(tags = "调度原则模块")
@Slf4j
@RestController
@RequestMapping("/principle")
public class SdPrincipleController {

    @Resource
    private SdPrincipleService sdPrincipleService;

    @RequestMapping(value = "/getPrinciple", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询调度原则")
    public R<PageVO> getPrinciple(@RequestBody SearchDTO searchDTO) {
        log.info("查询调度原则,查询条件：" + searchDTO);
        PageVO<SdPrincipleVO> principlelist = sdPrincipleService.getPrinciple(searchDTO);
        PageVO pageVO = BeanConvertUtils.convert(principlelist, PageVO.class);
        pageVO.setList(principlelist.getList());
        return R.success(pageVO);
    }

    @RequestMapping(value = "/getPrincipleByIds", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询没有加入调度方案的调度原则")
    public R<PageVO<SdPrincipleVO>> getPrincipleByIds(@RequestBody SearchDTO searchDTO, String ids) {
        log.info("查询没有加入调度方案的调度原则开始:");
        PageVO<SdPrincipleVO> principleByIds = sdPrincipleService.getPrincipleByIds(searchDTO, ids);
        PageVO pageVO = BeanConvertUtils.convert(principleByIds, PageVO.class);
        pageVO.setList(principleByIds.getList());
        log.info("查询没有加入调度方案的调度原则成功");
        return R.success(pageVO);
    }

    @RequestMapping(value = "/getBDValue", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取业务字典")
    public R<List<BDValueVo>> getBDValue(String typeCode) {
        if (typeCode == null || "".equals(typeCode)) {
            throw new GlobalException("请求参数无效");
        } else {
            log.info("获取业务字典开始:");
            List<BDValueVo> bdValueVoList = sdPrincipleService.getBDValue(typeCode);
            log.info("获取业务字典成功");
            return R.success(bdValueVoList);
        }
    }

    @RequestMapping(value = "/addPrinciple", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加调度原则")
    public R addPrinciple(@RequestBody SSPrincipleSaveDto ssPrincipleSaveDto) {
        log.info("添加调度原则开始:");
        try {
            sdPrincipleService.savePrinciple(ssPrincipleSaveDto);
            log.info("添加调度原则成功");
            return R.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("添加调度原则失败");
            return R.error();
        }
    }

    @RequestMapping(value = "/getPrinAttr", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询调度方案关联的原则源表")
    public R<List<PrincipleAttrVO>> getPrinAttr(String principleId) {
        if (principleId == null || "".equals(principleId) || Integer.parseInt(principleId) < 0) {
            throw new GlobalException("请求参数无效");
        } else {
            log.info("查询调度方案关联的原则源表开始:");
            List<PrincipleAttrVO> list = sdPrincipleService.getPrinAttr(Integer.parseInt(principleId));
            log.info("查询调度方案关联的原则源表成功");
            return R.success(list);
        }
    }

    @RequestMapping(value = "/getAttr", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询原则源表字段")
    public R<List<PrincipleAttrVO>> getAttr(String dictCode) {
        log.info("查询原则源表字段开始:");
        return R.success(sdPrincipleService.getAttr(dictCode));
    }

    @RequestMapping(value = "/updatePrinciple", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改调度原则")
    public R<String> updatePrinciple(@RequestBody SSPrincipleUpdateDto ssPrincipleUpdateDto) {
        log.info("修改调度原则开始, ssPrincipleUpdateDto : " + ssPrincipleUpdateDto);
        sdPrincipleService.updatePrinciple(ssPrincipleUpdateDto);
        return R.success("成功");
    }

    @RequestMapping(value = "/delPrinciple", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除调度原则")
    public R<String> delPrinciple(String principleId) {
        if (principleId == null || "".equals(principleId) || Integer.parseInt(principleId) < 0) {
            throw new GlobalException("请求参数无效");
        } else {
            log.info("删除调度原则开始");
            sdPrincipleService.delPrinciple(Integer.parseInt(principleId));
            log.info("删除调度原则成功");
            return R.success("成功");
        }
    }
}
